//
//  CViewController.m
//  A段项目
//
//  Created by cmcmhjr123 on 15/7/28.
//  Copyright (c) 2015年 dlios. All rights reserved.
//

#import "CViewController.h"
#import "BaseHeader.h"
#import "DataBaseHandler.h"
#import "AllCarTableViewCell.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "modelNew.h"

@interface CViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, retain) NSMutableArray *arr;
@property (nonatomic, assign) TableViewEditType isEdit;
@property (nonatomic, retain) UITableView *tab;


@end

@implementation CViewController

- (void)dealloc
{
    [super dealloc];
}
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        self.arr = [NSMutableArray array];
        self.isEdit = TableViewEditTypeIsNomal;
        
        UIBarButtonItem *bar = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(barAction:)];
        
        self.navigationItem.rightBarButtonItem = bar;

    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    DataBaseHandler *data = [DataBaseHandler shareInstance];

    self.arr =[data selectAllWithstr:@"collect"];
    
    
     self.tab = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeidth) style:UITableViewStylePlain];
    _tab.delegate = self;
    _tab.dataSource = self;
    _tab.rowHeight = 70;
    
    [_tab registerClass:[AllCarTableViewCell class] forCellReuseIdentifier:@"aa"];
    [self.view addSubview:_tab];

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
            AllCarTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"aa"];
    modelNew *model = [self.arr objectAtIndex:indexPath.row];
    NSString *str = model.Picture;
    if ([str containsString:@"{0}"]) {
        NSRange range = [str rangeOfString:@"{0}"];
        NSMutableString *str1 = [str mutableCopy];
        [str1 replaceCharactersInRange:range withString:@"3"];
        str = str1;
    }
    [cell.image sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"1.jpg"]];
    cell.nameLabel.text = model.serialName;
    
    return cell;
    
}


- (void)barAction:(id)sender
{
    if (self.isEdit == TableViewEditTypeIsEditing) {
        self.navigationItem.rightBarButtonItem.title = @"编辑";
        [_tab setEditing:NO animated:YES];
        self.isEdit = TableViewEditTypeIsNomal;
    }else if (self.isEdit == TableViewEditTypeIsNomal){
        self.navigationItem.rightBarButtonItem.title = @"完成";
        [_tab setEditing:YES animated:YES];
        self.isEdit = TableViewEditTypeIsEditing;
    }
    
}

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPa
{
    return UITableViewCellEditingStyleDelete;
}


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // 先删除数据
        
        modelNew *model  = [_arr objectAtIndex:indexPath.row];
        // 刷新界面
        DataBaseHandler *data = [DataBaseHandler shareInstance];
        
        [data deleteWithModel:model tablename:@"collect"];
        
        [_arr removeObjectAtIndex:indexPath.row];
        
        [_tab reloadData];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
